import { _decorator, Component, Sprite, UITransform, __private, Node } from 'cc';
import { DIRECTION_ENUM, EAT_STATUS_ENUM, PARAME_NAME_ENUM, PLAYER_ENUM } from "../../Enums/index"
import { IEnTity } from '../types/type';
import { PlayerStateMachine } from '../Player/PlayerStateMachine';
import DataManager from '../Runtime/DataManager';
import { getFrameAsset } from '../utils';
const { ccclass } = _decorator;

@ccclass('EntityManager')
export class EntityManager extends Component {


    fsm: PlayerStateMachine
    
    sprite: Sprite
    forcedWalking: Boolean = false //是否强制行走
    disabled: Boolean = false //判断是否可以控制移动 为true就不让移动
    end_w: number = 0  //移动最大的坐标
    end_a: number = 0
    end_s: number = 0
    end_d: number = 0
    targetX: number = 0 //移动到的目标X
    targetY: number = 0 //移动到的目标Y
    explosionTime: number = 10 //爆炸倒计时
    obstacle: Array<Node> = []// 面前是否有障碍物
    type:PLAYER_ENUM
    

    private _state: PARAME_NAME_ENUM = PARAME_NAME_ENUM.BOTTOM //动画状态

    private _direction: DIRECTION_ENUM = DIRECTION_ENUM.BOTTOM //方向

    private _eat_food: EAT_STATUS_ENUM = EAT_STATUS_ENUM.EMPTY //人物状态
    // 设置当前状态
    get state() {
        return this._state
    }
    set state(value: PARAME_NAME_ENUM) {
        this._state = value
        this.fsm.setParams(value, true)
    }
    // 设置方向
    get direction() {
        return this._direction
    }
    set direction(value: DIRECTION_ENUM) {
        this._direction = value
        this.fsm.setDirection(value)
    }

    // 设置吞食状态
    get eatFood() {
        return this._eat_food
    }
    set eatFood(value: EAT_STATUS_ENUM) {
        this._eat_food = value
        this.fsm.setEatFood(value)
        let roleEatStatus = DataManager.Instance.operationNode.getChildByName(this.type)
        const sprite = roleEatStatus.getChildByName("statusNode").getComponent(Sprite)
        sprite.spriteFrame = getFrameAsset(DataManager.Instance.homeAssets, `${value}.png`).spriteFrame
    }

    async init(params: IEnTity) {
        this.sprite = this.addComponent(Sprite)
        this.fsm = this.addComponent(params.fsm)
        this.fsm.init(params.frameList)
        const transforms = this.sprite.getComponent(UITransform)
        transforms.setAnchorPoint(0, 0.5);
        this.state = params.state
        this.direction = params.direction
    }
}


